package com.kong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kong.constant.MessageConstant;
import com.kong.entity.PageResult;
import com.kong.entity.QueryPageBean;
import com.kong.entity.Result;
import com.kong.pojo.CheckGroup;
import com.kong.service.CheckGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author yang2013
 * @data 2020/10/9 15:47
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 添加检查组
     * @param checkGroup
     * @param checkItemIds
     * @return
     */
    @PostMapping(value = "/add")
    public Result addGroup(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
        try {
            checkGroupService.addCheckGroup(checkGroup,checkItemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @GetMapping(value = "/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> checkGroupList=checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @GetMapping(value = "/delete")
    public Result deleteGroup(Integer id){
        try{
            checkGroupService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (RuntimeException r){
            return new Result(false, r.getMessage());
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    /**
     * 通过id查询检查组的
     * @param id
     * @return
     */
    @GetMapping(value = "/findById")
    public Result findListById(Integer id){
        try {
            Map map=checkGroupService.findListById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,map);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    /**
     * 编辑检查组
     * @param checkGroup
     * @return
     */
    @PostMapping(value = "/update")
    public Result updateGroup(Integer[] checkItemIds,@RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.updateCheckGroup(checkGroup,checkItemIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 分页查询
     */
    @PostMapping(value = "/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult byPage = checkGroupService.findByPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,byPage);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
